package com.lsa.skienna.challenge.chap1;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Scanner;

public class Mines implements Runnable{
	public static void main(String[] args) {
		new Mines().run();
	}
	
	static class MineSweeper{
		int[][] processTask(int[][] task){
			int n = task.length;
			int m = task[0].length;
			
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					if (task[i][j] == -1){// mine{
						for(int rowOffset = -1; rowOffset < 2; ++rowOffset){
							for(int colOffset = -1; colOffset < 2; ++colOffset ){
								int offI = i + rowOffset;
								int offJ = j + colOffset;
								if (offI > -1 && offI < n && offJ > -1 && offJ < m){
									if (task[offI][offJ] != -1) task[offI][offJ] += 1;  // do not touch the mines
								}
							}
						}
					}
				}
			}
			
			return task;
		}
	}
	
	static class TaskReader{
		private BufferedReader reader;
		
		TaskReader(InputStream stream) {
			this.reader = new BufferedReader(new InputStreamReader(stream));
		}
		
		int[][] readNextTask() throws IOException{
			int[][] task = null;
			
			String line = reader.readLine();
			if (line == null){
				return null;
			}
			
			Scanner scanner = new Scanner(line);
			int n = 0;
			int m = 0;
			
			try{
				n = scanner.nextInt();  
				scanner.skip(" ");
				m = scanner.nextInt();  
				
				if (!checkDim(n) || !checkDim(n)) return null;
				
				task = new int[n][m];
				
				for(int i = 0; i < n; ++i){
					line = reader.readLine();
					if (line == null || line.length() < m){
						return null;
					}
					for(int j = 0; j < m; ++j){
						char c = line.charAt(j);
						int val;
						if (c == '*'){
							val = -1; // mine
						}
						else if (c == '.'){
							val = 0; // clear, mine counter
						}
						else return null;
						
						task[i][j] = val;
					}
				}
			}
			catch(RuntimeException e){
				return null;
			}
			
			return task;
		}
		
		static boolean checkDim(int dim){
			return dim > 0 && dim <= 100;
		}
	}
	
	static class ResultWriter{
		private static final String lineSeparator = System.getProperty("line.separator");
		
		private BufferedWriter writer;
		private int fieldNo = 1;
		ResultWriter(OutputStream stream) {
			this.writer = new BufferedWriter(new OutputStreamWriter(stream));
		}
		
		void write(int[][] processedTask) throws IOException{
			int n = processedTask.length;
			int m = processedTask[0].length;
			
			this.writer.write("Field #" + (fieldNo++) + ":" + lineSeparator);
			
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < m; j++) {
					int val = processedTask[i][j];
					if (val == -1){
						writer.write('*');
					}
					else{
						writer.write(""+val);
					}
				}
				writer.write(lineSeparator);
				writer.flush();
			}
			writer.write(lineSeparator);
		}
	}

	@Override
	public void run() {
		try {
			TaskReader taskReader = new TaskReader(System.in);
			ResultWriter resultWriter = new ResultWriter(System.out);
			int[][] task;
			while( (task = taskReader.readNextTask()) != null){
				MineSweeper sweeper = new MineSweeper();
				int[][] result = sweeper.processTask(task);
				
				resultWriter.write(result);
			}
		} catch (IOException e) {
		}
	}
}
